home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume10 / weekday.c < prev    next >
Encoding:
Text File  |  1990-01-28  |  27.6 KB  |  723 lines

  1. Newsgroups: comp.sources.misc
  2. organization: Delft University of Technology, Dep. of Electrotechnical engineering.
  3. subject: v10i047: weekday: gives info on dates: like weeknr and weekday
  4. From: marcel@duteca.tudelft.nl (Marcel Mol)
  5. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  6.  
  7. Posting-number: Volume 10, Issue 47
  8. Submitted-by: marcel@duteca.tudelft.nl (Marcel Mol)
  9. Archive-name: weekday.c
  10.  
  11. #!/bin/sh
  12. # This is a shell archive, meaning:
  13. # 1. Remove everything above the #!/bin/sh line.
  14. # 2. Save the resulting text in a file.
  15. # 3. Execute the file with /bin/sh (not csh) to create the files:
  16. #     weekday.c        26078    bytes
  17. # This archive created:  Fri Jan 26 00:38:00 MET 1990
  18. # By:     Marcel J.E. Mol
  19. export PATH
  20. echo shar: extracting weekday.c
  21. if test -f 'weekday.c'
  22. then
  23.     echo shar: over-writing existing file "'weekday.c'"
  24. fi
  25. sed -e 's/^-//' << \_E_O_F_ > weekday.c
  26. -
  27. -/* @(#) weekday.c       3.31 09/01/90  */
  28. -/*************************************************************************
  29. - **                                                                     **
  30. - **  Name    :  weekday                                                 **
  31. - **  Author  :  Marcel J.E. Mol                                         **
  32. - **  Date    :  12/28/89              (first release)                   **
  33. - **  Version :  3.25                                                    **
  34. - **  Files   :  weekday.c       Main source file                        **
  35. - **                                                                     **
  36. - **  ------------------------- Revision List -------------------------  **
  37. - **  Ver   Date       Name                   Remarks                    **
  38. - **  3.31  09/01/90   Marcel Mol    Removed unnecessary code.           **
  39. - **                                 Added comments.                     **
  40. - **  3.30  08/01/90   Marcel Mol    Improved option handling.           **
  41. - **                                 verbose works on more options.      **
  42. - **                                 use printf field length option to   **
  43. - **                                 get short day and month names.      **
  44. - **  3.26  07/01/90   Marcel Mol    Filled in usage function.           **
  45. - **  3.25  04/01/90   Marcel Mol    Use 'today' when no date is given.  **
  46. - **  3.20  03/01/90   Marcel Mol    Allow bsd `date` as input.          **
  47. - **  3.11  03/01/90   Marcel Mol    Documentation, typos, checkdate().  **
  48. - **                                 Removed redundant checks.           **
  49. - **  3.10  02/01/90   Marcel Mol    Added -s and -v options.            **
  50. - **                                 allow yy.ddd input and implement    **
  51. - **                                 julian date conversion.             **
  52. - **  3.00  12/30/89   Marcel Mol    Set up user interface.              **
  53. - **                                 Allow mm/dd/yy and dd-mm-yy input.  **
  54. - **                                 Options to print different parts.   **
  55. - **  2.10  12/28/89   Marcel Mol    Added Julianday.                    **
  56. - **  2.00  12/27/89   Marcel Mol    Made weekday and weeknr everlasting.**
  57. - **  1.00  12/26/89   Marcel Mol    First release.                      **
  58. - **                                 Set up algorithms weekday weeknr.   **
  59. - **  =================================================================  **
  60. - **                                                                     **
  61. - **  Compile as follows: cc weekday.c -Oso weekday                      **
  62. - **                                                                     **
  63. - **  Usage: weekday [-svdDmMywjnah] [<date>]                            **
  64. - **                                                                     **
  65. - **         Weekday outputs line on which it informs you about several  **
  66. - **         thing of the given <date>.                                  **
  67. - **         The <date> input may be in several formats:                 **
  68. - **                                                                     **
  69. - **             mm/dd/yy      e.g.   10/30/89    or 1/2/510             **
  70. - **             dd-mm-yy      e.g.   30-10-89    or 2-1-510             **
  71. - **             yy.ddd        e.g.   89.303      or 510.2               **
  72. - **             `date` output e.g.   Mon Oct 30 99:99:99 1989           **
  73. - **                                                                     **
  74. - **         If no date argument is given, weekday uses the system date. **
  75. - **         When the year number is less than 100 and the string        **
  76. - **         forming the year does not start with a 0, the year is       **
  77. - **         converted to be in the current century. So 10/30/89 is      **
  78. - **         actually 10/30/1989. If the real year 89 is wanted specify  **
  79. - **         it as 10/30/089. (the current century is a constant in the  **
  80. - **         source code. If this programs survives 1999 this constant   **
  81. - **         must be changed, and the program recompiled.)               **
  82. - **         Note: make sure that `date` output is passed as one         **
  83. - **         argument, so enclose it with "s.                            **
  84. - **                                                                     **
  85. - **         The output, when no options are given, is something like    **
  86. - **         this:                                                       **
  87. - **                                                                     **
  88. - **         30 October 1989 is on a Monday on julian day 303 in week 44 **
  89. - **                                                                     **
  90. - **         or:                                                         **
  91. - **                                                                     **
  92. - **         2 Januari 510 is on a Friday on julian day 2 in week 1      **
  93. - **                                                                     **
  94. - **         Depending on the options the output changes:                **
  95. - **                                                                     **
  96. - **           -s toggle short form output: Monday -> Mon, June -> Jun   **
  97. - **              and vice versa.                                        **
  98. - **           -v toggle verbose output; removes 'is on a' etc.          **
  99. - **              and vice versa. (Only has effect on the default case   **
  100. - **              and on the -a option.)                                 **
  101. - **           -d prints day of week number: 1 is Monday, 7 is Sunday    **
  102. - **           -D gives day of week name, i.e Monday or Mon              **
  103. - **           -m prints the month number                                **
  104. - **           -M outputs the month name, i.e. Januari or Jan            **
  105. - **           -y outputs the year number                                **
  106. - **           -w gives the week number                                  **
  107. - **           -j outputs the julian day number                          **
  108. - **           -n gives day of month                                     **
  109. - **           -a gives everything it knows. Without the -s and -v       **
  110. - **              options this outputs the same as the default case.     **
  111. - **              In fact -a is just short for -nmyDjw.                  ** 
  112. - **           -h give a usage message                                   **
  113. - **                                                                     **
  114. - **         All options that print something also print a space         **
  115. - **         character (except -a).                                      **
  116. - **         The options may come in any order and as many times as      **
  117. - **         you like. The -s and -v options only have an effect on the  **
  118. - **         options that follow them.                                   **
  119. - **         When now option is given the -a option is default.          **
  120. - **         The output is ended with newline                            **
  121. - **         Examples:                                                   **
  122. - **                                                                     **
  123. - **           % weekday  -yjMdD 12/31/89                                **
  124. - **           89 365 December 7 Sunday                                  **
  125. - **                                                                     **
  126. - **           % weekday  -yjMsdD 12/31/89                               **
  127. - **           89 365 December 7 Sun                                     **
  128. - **                                                                     **
  129. - **           % weekday  -yjsMsdD 12/31/89                              **
  130. - **           89 365 Dec 7 Sunday                                       **
  131. - **                                                                     **
  132. - **           % weekday  -v 30/30/89                                    **
  133. - **           30 Oct 1989 is on a Mon on julian day 303 in week 44      **
  134. - **                                                                     **
  135. - **   BUGS: the program assumes there have always been leap years       **
  136. - **         so year 4 is a leap year as far as the program can tell...  **
  137. - **                                                                     **
  138. - **   This software is copyrighted (c) 1989 1990 by M.J.E. Mol.         **
  139. - **   This code or part of this code may be used by freeware,           **
  140. - **   public domain or any other non-commercial product. I only ask     **
  141. - **   you to give me credit for the used code in the documentation      **
  142. - **   and in your source code.                                          **
  143. - **                                                                     **
  144. - **   If you want to use this code or part of this code in your         **
  145. - **   shareware or any other commercial product, I ask you to contact   **
  146. - **   me so we can setup an arrangement for the use of the code or part **
  147. - **   of the code.                                                      **
  148. - **                                                                     **
  149. - **   In any case, I can not be held responsible for any damage this    **
  150. - **   code or part of this code may cause you or anyone or anything     **
  151. - **   else.                                                             **
  152. - **                                                                     **
  153. - **   Mail bugs to: email: marcel@duteca.tudelft.nl                     **
  154. - **                 pmail: Marcel J.E. Mol                              **
  155. - **                        Karel Doormanlaan 58                         **
  156. - **                        2283AT Rijswijk                              **
  157. - **                        The Netherlands (Holland)                    **
  158. - **                 Phone: (+31) 070-3932390                            **
  159. - **                 Bankaccount: 36.89.35.922 (In the Netherlands)      **
  160. - **                                                                     **
  161. - ************************************************************************/
  162. -
  163. -char * copyright = "@(#) weekday.c  3.31 09/01/90  (c) M.J.E. Mol";
  164. -#include <stdio.h>
  165. -#include <string.h>
  166. -#include <time.h>
  167. -
  168. -
  169. -#define CENTURY     1900                             /* prefix for years */
  170. -#define LEAP(year)  ((year%4 == 0) && (year%100 != 0) || (year%400 == 0)) 
  171. -
  172. -/*
  173. - * Global variables
  174. - */
  175. -
  176. -char *weekdays[8]   = {"", "Monday", "Tuesday", "Wednesday", "Thursday",
  177. -                           "Friday", "Saterday", "Sunday"};
  178. -char  days[13]      = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  179. -int   juldays[13]   = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  180. -char *monthname[13] = {"", "Januari", "Februari", "March", "April",
  181. -                           "May", "June", "July", "August",
  182. -                           "September", "Oktober", "November", "December"};
  183. -
  184. -int shortflag = 0;                  /* short output of day and month name */
  185. -int verbose   = 1;                  /* output in a 'sentence' */
  186. -char * progname;
  187. -
  188. -
  189. -/*
  190. - * function declarations
  191. - */
  192. -void main();
  193. -void scanmmddyy();
  194. -void scanddmmyy();
  195. -int  scanjul();
  196. -int  scandate();
  197. -int  julianday();
  198. -void julian2day();
  199. -int  weekday();
  200. -int  weeknr();
  201. -void checkdate();
  202. -void usage();
  203. -
  204. -
  205. -void main(argc, argv)
  206. -int argc;
  207. -char **argv;
  208. -{
  209. -    int year, month, day, week;
  210. -    int wday = -1;
  211. -    int jday = -1;
  212. -    int opt, haveopt;
  213. -    char *date;
  214. -    extern int optind;                  /* For getopt */
  215. -    extern char * optarg;               /* For getopt */
  216. -
  217. -    progname = *argv;
  218. -    date = argv[argc-1];
  219. -
  220. -    /*
  221. -     * pick up the date argument and scan it
  222. -     */
  223. -    if (*date == '-' || argc == 1)                   /* no date, take current */
  224. -        wday = scandate(NULL, &year, &month, &day);
  225. -    else if (strchr(date, '/') != NULL)              /* mm/dd/yy format date */
  226. -        scanmmddyy(date, &year, &month, &day);
  227. -    else if (strchr(date, '-') != NULL)              /* dd-mm-yy format date */
  228. -        scanddmmyy(date, &year, &month, &day);
  229. -    else if (strchr(date, '.') != NULL)              /* yyyy.ddd format date */
  230. -        jday = scanjulian(date, &year, &month, &day);
  231. -    else                                           /* unix date output format */
  232. -        wday = scandate(date, &year, &month, &day);
  233. -
  234. -    /*
  235. -     * get missing data
  236. -     */
  237. -    if (jday == -1)
  238. -        jday = julianday(year, month, day);
  239. -    if (wday == -1)
  240. -        wday = weekday(year, month, day);
  241. -    week = weeknr(year, month, day);
  242. -
  243. -
  244. -    /*
  245. -     * do the output that the user wants...
  246. -     */
  247. -    while ((opt = getopt(argc, argv, "svdDmMywjnah")) != EOF) {
  248. -        switch (opt) {
  249. -            case 's' : shortflag ^=  1;
  250. -                       break;
  251. -            case 'v' : verbose ^= 1;
  252. -                       break;
  253. -            case 'D' : printf("%s%.*s ",
  254. -                                   verbose ? "is on a " : "",
  255. -                                   shortflag ? 3 : 9 , weekdays[wday]);
  256. -                       haveopt = 1;
  257. -                       break;
  258. -            case 'd' : printf("%s%d ", verbose ? "is weekday " : "", wday);
  259. -                       haveopt = 1;
  260. -                       break;
  261. -            case 'm' : printf("%d ", month);
  262. -                       haveopt = 1;
  263. -                       break;
  264. -            case 'M' : printf("%.*s ", shortflag ? 3 : 9, monthname[month]);
  265. -                       haveopt = 1;
  266. -                       break;
  267. -            case 'y' : printf("%d ", year);
  268. -                       haveopt = 1;
  269. -                       break;
  270. -            case 'w' : printf("%s%d ", verbose ? "in week " : "", week);
  271. -                       haveopt = 1;
  272. -                       break;
  273. -            case 'j' : printf("%s%d ", verbose ? "on julian day " : "", jday);
  274. -                       haveopt = 1;
  275. -                       break;
  276. -            case 'n' : printf("%d ", day);
  277. -                       haveopt = 1;
  278. -                       break;
  279. -            case 'a' : if (verbose) {
  280. -                           printf("%d %.*s %d is on a %.*s ",
  281. -                                  day,  shortflag ? 3 : 9, monthname[month],
  282. -                                  year, shortflag ? 3 : 9, weekdays[wday]);
  283. -                           printf("on julian day %d in week %d", jday, week);
  284. -                       }
  285. -                       else
  286. -                           printf("%d %.*s %d %.*s %d %d",
  287. -                                  day,  shortflag ? 3 : 9, monthname[month],
  288. -                                  year, shortflag ? 3 : 9, weekdays[wday],
  289. -                                  jday, week);
  290. -                       haveopt = 1;
  291. -                       break;
  292. -            case 'h' : usage();
  293. -                       break;
  294. -            default  : fprintf(stderr,
  295. -                               "%s: use -h option for help\n", progname);
  296. -                       exit(1);
  297. -                       break;
  298. -        }
  299. -    }
  300. -
  301. -
  302. -    if (!haveopt) {
  303. -        if (verbose) {
  304. -            printf("%d %.*s %d is on a %.*s ",
  305. -                   day,  shortflag ? 3 : 9, monthname[month],
  306. -                   year, shortflag ? 3 : 9, weekdays[wday]);
  307. -            printf("on julian day %d in week %d", jday, week);
  308. -        }
  309. -        else
  310. -            printf("%d %.*s %d %.*s %d %d",
  311. -                   day,  shortflag ? 3 : 9, monthname[month],
  312. -                   year, shortflag ? 3 : 9, weekdays[wday],
  313. -                   jday, week);
  314. -    }
  315. -
  316. -    /*
  317. -     * finish with a newline
  318. -     */
  319. -    putchar('\n');
  320. -
  321. -    exit(0);
  322. -
  323. -} /* main */
  324. - /*************************************************************************
  325. - **                                                                     **
  326. - **                    DATE SCANNING ROUTINES                           **
  327. - **                                                                     **
  328. - *************************************************************************/
  329. -
  330. -/*
  331. - * scanmmddyy --- scan the date variable in the form MM/DD/YY.
  332. - *                MM, DD and YY may be variable sized numbers.
  333. - *                output is in output variables year, month and day.
  334. - */
  335. -void scanmmddyy(date, year, month, day)
  336. -char *date;
  337. -int *year, *month, *day;
  338. -{
  339. -    char *i;
  340. -
  341. -    if (sscanf(date, "%d/%d/%d", month, day, year) != 3 ||
  342. -        *month <= 0 || *day <= 0 || *year < 0) {
  343. -        fprintf(stderr, "%s: error in mm/dd/yy format date %s\n",
  344. -                        progname, date);
  345. -        exit(4);
  346. -    }
  347. -
  348. -    if (*year < 100) {
  349. -        i = strrchr(date, '/');
  350. -        if (*i != 0)
  351. -           *year += CENTURY;
  352. -    }
  353. -
  354. -    return;
  355. -
  356. -} /* scanmmddyy */
  357. -
  358. -
  359. -
  360. -/*
  361. - * scanddmmyy --- scan the date variable in the form DD-MM-YY.
  362. - *                DD, MM and YY may be variable sized numbers.
  363. - *                output is in output variables year, month and day.
  364. - */
  365. -void scanddmmyy(date, year, month, day)
  366. -char *date;
  367. -int *year, *month, *day;
  368. -{
  369. -    char *i;
  370. -
  371. -    if (sscanf(date, "%d-%d-%d", day, month, year) != 3 ||
  372. -        *month <= 0 || *day <= 0 || *year < 0) {
  373. -        fprintf(stderr, "%s: error in dd-mm-yy format date %s\n",
  374. -                        progname, date);
  375. -        exit(4);
  376. -    }
  377. -
  378. -    if (*year < 100) {
  379. -        i = strrchr(date, '-');
  380. -        if (*i != 0)
  381. -           *year += CENTURY;
  382. -    }
  383. -
  384. -    return;
  385. -
  386. -} /* scanddmmyy */
  387. -
  388. -
  389. -
  390. -/*
  391. - * scanjulian --- scan the date variable in the form YY.DDD.
  392. - *                YY and DDD may be variable sized numbers.
  393. - *                output is in output variables year, month and day.
  394. - *                the return value is tha actual julian day DDD.
  395. - */
  396. -int scanjulian(date, year, month, day)
  397. -char *date;
  398. -int *year, *month, *day;
  399. -{
  400. -    int jday;
  401. -
  402. -    if (sscanf(date, "%d.%d", year, &jday) != 2 || *year < 0 || jday <= 0) {
  403. -        fprintf(stderr, "%s: error in julian date %s\n", progname, date);
  404. -        exit(4);
  405. -    }
  406. -
  407. -    if ((*year < 100) && (*date != '0'))
  408. -        *year += CENTURY;
  409. -    julian2day(*year, jday, month, day);
  410. -
  411. -    return jday;
  412. -
  413. -} /* scanjulian */
  414. -
  415. -
  416. -
  417. -/*
  418. - * scandate --- scan the date variable in the Unix `date` form.
  419. - *              This can look like: "Mon Jan 8 20:10:10 MET 1990"
  420. - *              or:                 "Mon Jan 8 20:10:10 1990"
  421. - *              the number of spaces between the fields is not fixed
  422. - *              (although date output is always in  the same with, see ctime(3).
  423. - *              output is in output variables year, month and day.
  424. - *              the return value is the day of the week number.
  425. - *              If the date input is the NULL pointer, the xurrent system date
  426. - *              is taken.
  427. - */
  428. -int scandate(date, year, month, day)
  429. -char *date;
  430. -int *year, *month, *day;
  431. -{
  432. -    char *i;
  433. -    char dayname[4];
  434. -    char monname[10];
  435. -    int j, k;
  436. -    long tim;
  437. -
  438. -    if (date == NULL) {
  439. -        tim = time((long *) 0);
  440. -        date= ctime(&tim);
  441. -    }
  442. -
  443. -    if (sscanf(date, "%3s %3s %d %*s %d",      /* date without time zone */
  444. -                     dayname, monname, day, year) != 4 &&
  445. -        sscanf(date, "%3s %3s %d %*s %*s %d", /* date with time zone */
  446. -                     dayname, monname, day, year) != 4) {
  447. -        fprintf(stderr, "%s: error in unix `date` format date %s\n",
  448. -                        progname, date);
  449. -        exit(4);
  450. -    }
  451. -
  452. -    for(j=1; j<12; j++) {
  453. -        if (strncmp(monname, monthname[j], 3) == NULL)
  454. -            break;
  455. -    }
  456. -    if (strncmp(monname, monthname[j], 3) != NULL) {
  457. -        fprintf(stderr, "%s: error in unix `date` format date %s\n",
  458. -                        progname, date);
  459. -        exit(4);
  460. -    }
  461. -    *month = j;
  462. -    for(j=1; j<7; j++) {
  463. -        if (strncmp(dayname, weekdays[j], 3) == NULL)
  464. -            break;
  465. -    }
  466. -    if (strncmp(dayname, weekdays[j], 3) != NULL) {
  467. -        fprintf(stderr, "%s: error in unix `date` format date %s\n",
  468. -                        progname, date);
  469. -        exit(4);
  470. -    }
  471. -
  472. -    return j;
  473. -
  474. -} /* scandate */
  475. - /*************************************************************************
  476. - **                                                                     **
  477. - **                    DATE CONVERSION ROUTINES                         **
  478. - **                                                                     **
  479. - *************************************************************************/
  480. -
  481. -
  482. -
  483. -/* 
  484. - * julian2day -- converts year and julian day value to the corresponding 
  485. - *               month and day of the month values for that year.
  486. - *               these values are returned in the ouput variables month and day.
  487. - */
  488. -void julian2day(year, jday, month, day)
  489. -int year, jday;
  490. -int *month, *day;
  491. -{
  492. -    int leap;
  493. -
  494. -    leap = LEAP(year) ? 1 : 0;
  495. -    if (jday > 365+leap || jday <= 0) {
  496. -        fprintf(stderr, "%s: error in julian date %d.%d\n",
  497. -                        progname, year, jday);
  498. -       exit(8);
  499. -    }
  500. -
  501. -    if (jday <= 31) {
  502. -        *month = 1;
  503. -        *day = jday;
  504. -    }
  505. -    else if (jday <= 59+leap) {
  506. -        *month = 2;
  507. -        *day = jday - 31;
  508. -    }
  509. -    else if (jday <= 90+leap) {
  510. -        *month = 3;
  511. -        *day = jday - 59 - leap;
  512. -    }
  513. -    else if (jday <= 120+leap) {
  514. -        *month = 4;
  515. -        *day = jday - 90 - leap;
  516. -    }
  517. -    else if (jday <= 151+leap) {
  518. -        *month = 5;
  519. -        *day = jday - 120 - leap;
  520. -    }
  521. -    else if (jday <= 181+leap) {
  522. -        *month = 6;
  523. -        *day = jday - 151 - leap;
  524. -    }
  525. -    else if (jday <= 212+leap) {
  526. -        *month = 7;
  527. -        *day = jday - 181 - leap;
  528. -    }
  529. -    else if (jday <= 243+leap) {
  530. -        *month = 8;
  531. -        *day = jday - 212 - leap;
  532. -    }
  533. -    else if (jday <= 273+leap) {
  534. -        *month = 9;
  535. -        *day = jday - 243 - leap;
  536. -    }
  537. -    else if (jday <= 304+leap) {
  538. -        *month = 10;
  539. -        *day = jday - 273 - leap;
  540. -    }
  541. -    else if (jday <= 334+leap) {
  542. -        *month = 11;
  543. -        *day = jday - 304 - leap;
  544. -    }
  545. -    else {
  546. -        *month = 12;
  547. -        *day = jday - 334 - leap;
  548. -    }
  549. -
  550. -    return;
  551. -
  552. -} /* julian2day */
  553. -
  554. -
  555. -
  556. -/* 
  557. - * julianday -- converts a year, mont and day value the corresponding julian
  558. - *              day number of that year.
  559. - *              the julian day is passed as a return value.
  560. - */
  561. -julianday(year, month, day)
  562. -int year, month, day;
  563. -{
  564. -    register int doy;
  565. -
  566. -    checkdate(year, month, day);           /* exits if date in error */
  567. -    doy = juldays[month];
  568. -    if ((month > 2) && LEAP(year)) 
  569. -        doy++;
  570. -    doy += day;
  571. -
  572. -    return doy;
  573. -
  574. -} /* julianday */
  575. -
  576. -
  577. -
  578. -/*
  579. - * weekday -- returns the day of the week number based on the given
  580. - *            year, month and day of the month value.
  581. - */
  582. -weekday(year, month, day)
  583. -int year, month, day;
  584. -{
  585. -    register int dow;
  586. -
  587. -    checkdate(year, month, day);           /* exits if date in error */
  588. -    dow = 6 +
  589. -          year +                          /* shift a day each year */
  590. -          year/4 - year/100 + year/400 +  /* shift a day each leap year */
  591. -          juldays[month] + day;
  592. -
  593. -    if ((month < 3) && LEAP(year))        /* first two months of leap don't */
  594. -        dow--;                            /* have the extra leap shift      */
  595. -
  596. -    dow %= 7;
  597. -    return (dow == 0) ? 7 : dow;
  598. -
  599. -} /* weekday */
  600. -
  601. -
  602. -/*
  603. - * weeknr -- returns the weeknumber of the given date (year, month and day of
  604. - *           the month) for that year.
  605. - */
  606. -weeknr(year, month, day)
  607. -int year, month, day;
  608. -{
  609. -    register int fday, jul, weeknum;
  610. -
  611. -    checkdate(year, month, day);           /* exits if date in error */
  612. -
  613. -    fday = weekday(year, 1, 1);
  614. -
  615. -    jul = julianday(year, month, day);
  616. -    weeknum = (jul + fday - 2)/7;
  617. -    if (fday <= 4)
  618. -        weeknum++;
  619. -
  620. -    if (weeknum == 0)                      /* go into previous year           */
  621. -        if ((fday == 5) ||                 /* week 1 has 4 or 5 days and week */
  622. -           ((fday == 6) && LEAP(year-1)) ) /*    53 has 4 or 5 days in year-1 */
  623. -            return 53;
  624. -        else
  625. -            return 52;
  626. -
  627. -    if (weeknum == 53) {                  /* only a week 53 if we have > 4   */
  628. -        fday = weekday(year, 12, 31);     /*     days in the week            */
  629. -        if (fday < 4)
  630. -           weeknum = 1;
  631. -    }
  632. -
  633. -    return weeknum;
  634. -
  635. -} /* weeknr */
  636. - /*************************************************************************
  637. - **                                                                     **
  638. - **                         OTHER  ROUTINES                             **
  639. - **                                                                     **
  640. - *************************************************************************/
  641. -
  642. -
  643. -
  644. -/*
  645. - * checkdate -- checks if the given date is a valid one.
  646. - *              if not, it just exits.
  647. - */
  648. -void checkdate(year, month, day)
  649. -int year, month, day;
  650. -{
  651. -    int dim;
  652. -
  653. -    if (year < 0) {
  654. -        fprintf(stderr, "%s: %d is an invalid year\n", progname, year);
  655. -        exit(2);
  656. -    }
  657. -    if ((month < 1) || (month > 12)) {
  658. -        fprintf(stderr, "%s: %d is an invalid month\n", progname, month);
  659. -        exit(2);
  660. -    }
  661. -    dim = days[month];
  662. -    if ((month == 2) && LEAP(year)) 
  663. -        dim++;
  664. -    if ((day > dim) || (day < 1)) {
  665. -        fprintf(stderr, "%s: %d is an invalid day in %s %d\n", progname,
  666. -                         day, monthname[month], year);
  667. -        exit(2);
  668. -    }
  669. -
  670. -   return;
  671. -
  672. -} /* checkdate */
  673. -
  674. -
  675. -
  676. -/*
  677. - * usage -- give usage message.
  678. - */
  679. -void usage()
  680. -{
  681. -
  682. -    fprintf(stderr,"Usage: weekday [-svdDmMywjnah] [<date>]\n"); 
  683. -    fprintf(stderr,"   <date>: mm/dd/yy    dd-mm-yy    yy.ddd   Unix date\n");
  684. -    fprintf(stderr,"           default is today\n");
  685. -    fprintf(stderr,"   options: -s toggle short form output\n");
  686. -    fprintf(stderr,"            -v toggle verbose output\n");
  687. -    fprintf(stderr,"            -d prints day of week number\n");
  688. -    fprintf(stderr,"            -D gives day of week name\n");
  689. -    fprintf(stderr,"            -m prints the month number\n");
  690. -    fprintf(stderr,"            -M outputs the month name\n");
  691. -    fprintf(stderr,"            -y outputs the year number\n");
  692. -    fprintf(stderr,"            -w gives the week number\n");
  693. -    fprintf(stderr,"            -j outputs the julian day number\n");
  694. -    fprintf(stderr,"            -n gives day of month\n");
  695. -    fprintf(stderr,"            -a gives everything it knows (default)\n");
  696. -    fprintf(stderr,"            -h give a usage message\n");
  697. -
  698. -    return;
  699. -
  700. -} /* usage */
  701. _E_O_F_
  702. if test 26078 -ne `wc -c < weekday.c `; then
  703.     echo shar: "'weekday.c'" unpacked with wrong size!
  704. fi
  705. # end of shell archive
  706. exit 0
  707. -- 
  708. #########################################
  709. # Marcel J.E. Mol                       ######################################
  710. # Delft University of Technology          Pink Elephant Management Services  #
  711. # The Netherlands                         Voorburg                           #
  712. # UUCP: marcel@duteca.tudelft.nl          Tel: 070-694231                    #
  713. #                                          ######################################
  714. #########################################
  715.  
  716.  
  717. ----- End Forwarded Message -----
  718.  
  719.